package com.atguigu.ssyx.api;

import com.atguigu.ssyx.model.ZhgjRequest;
import com.atguigu.ssyx.model.ZhgjResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


/**
 * 智慧规建系统API
 */
@Component
@Slf4j
public class ZhgjApi {
    // 环境配置
    private static final String SERVER_SIT_URL = "https://msp-miniapp-sit.teleone.com.cn";
    private static final String TOKEN_SIT = "{{a626df4849ec3ffff7728ac165dcd952}}";
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 发送POST请求到智慧规建系统
     *
     * @param endpoint    接口端点
     * @param requestBody 请求体内容
     * @return 响应结果字符串
     */
    private ZhgjResponse postToZhgj(String endpoint, ZhgjRequest requestBody) {
        String url = SERVER_SIT_URL + endpoint;
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Content-Type", "application/json");
        // 创建请求实体
        HttpEntity requestEntity = new HttpEntity(requestBody, headers);
        // 发送请求
        ZhgjResponse response = restTemplate.postForObject(url, requestEntity, ZhgjResponse.class);
        return response;
    }

    /**
     * 通过手机号核验接口
     *
     * @param phoneNumber 手机号码
     * @return 核验结果
     */
    public ZhgjResponse checkPhone(String phoneNumber) {
        ZhgjRequest requestBody = new ZhgjRequest();
        requestBody.setPhoneNumber(phoneNumber);
        requestBody.setToken(TOKEN_SIT);
        ZhgjResponse response = postToZhgj("/ext_api/cluster/center/api/checkPhone", requestBody);
        return response;
    }

    /**
     * 通过身份证核验接口
     *
     * @param idNumber 身份证号码
     * @return 核验结果
     */
    public ZhgjResponse checkIdNumber(String idNumber) {
        ZhgjRequest requestBody = new ZhgjRequest();
        requestBody.setIdNumber(idNumber);
        requestBody.setToken(TOKEN_SIT);
        ZhgjResponse response = postToZhgj("/ext_api/cluster/center/api/checkIdNumber", requestBody);
        return response;
    }

    /**
     * 通过二维码核验接口
     *
     * @param qrCodeUrl 二维码内容
     * @return 核验结果
     */
    public ZhgjResponse checkByQrCode(String qrCodeUrl) {
        ZhgjRequest requestBody = new ZhgjRequest();
        requestBody.setUrl(qrCodeUrl);
        requestBody.setToken(TOKEN_SIT);
        ZhgjResponse response = postToZhgj("/ext_api/cluster/center/api/checkByQrCode", requestBody);
        return response;
    }

    /**
     * 预约到访确认接口
     *
     * @param bookingId 预约记录ID
     * @return 确认结果
     */
    public ZhgjResponse checkVisit(String bookingId) {
        ZhgjRequest requestBody = new ZhgjRequest();
        requestBody.setId(bookingId);
        requestBody.setToken(TOKEN_SIT);
        ZhgjResponse response = postToZhgj("/ext_api/cluster/center/api/checkVisit", requestBody);
        return response;
    }

    /**
     * 预约记录查询接口
     *
     * @param phone 手机号码
     * @return 预约记录列表
     */
    public ZhgjResponse bookingVisitList(String phone) {
        ZhgjRequest requestBody = new ZhgjRequest();
        requestBody.setPhoneNumber(phone);
        requestBody.setToken(TOKEN_SIT);
        ZhgjResponse response = postToZhgj("/ext_api/cluster/center/api/bookingVisitList", requestBody);
        return response;
    }

    public Boolean judgeStatus(ZhgjResponse response) {
        boolean isSuccess = true;
        if (!"00000000".equals(response.getCode())) {
            // 根据指令要求，使用返回的code和message抛出异常
            log.info("API调用失败: {} (错误码: {})", response.getMessage(), response.getCode());
            isSuccess = false;
        }
        return isSuccess;
    }

    /**
     * 自定义异常类
     */
    public static class ZhgjApiException extends RuntimeException {
        public ZhgjApiException(String message) {
            super(message);
        }
    }
}